﻿using Microsoft.Extensions.DependencyInjection;
using Polly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using XjjXmm.FrameWork.ToolKit;

namespace XjjXmm.Spider.biquge.BiqupaiCom;

internal class SetUpProfile : AbstractSetUpProfile<BiqupaiComSpider>
{
    //public static readonly string domain = "https://www.biqupai.com";

    //public static readonly List<string> keys = new List<string> { GuidKit.Get() };

    private HttpClientHandler BuildHttpClientHandler()
    {
        return new HttpClientHandler()
        {
            AutomaticDecompression = System.Net.DecompressionMethods.All,
        };
    }

    protected override void ConfigClient(string key, IServiceCollection services, string? proxy = null)
    {
        var httpClientHandler = BuildHttpClientHandler();
        if (!string.IsNullOrEmpty(proxy))
        {
            httpClientHandler.Proxy = new WebProxy(proxy);
            httpClientHandler.UseProxy = true;
        }


        var httpClientBuild = services.AddHttpClient(key, client =>
        {
            client.BaseAddress = new Uri(domain);

            client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
        })
         .AddHttpMessageHandler<HttpLogHandel<BiqupaiComSpider>>()
        // .AddHttpMessageHandler<HttpGBKHandel>()
         .AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)))
        .ConfigurePrimaryHttpMessageHandler(() => httpClientHandler);
    }

    //public IServiceCollection Regist(IServiceCollection services)
    //{
    //    services.AddScoped<ISiteSpider, BiqupaiComSpider>();
    //    services.AddSingleton<HttpLogHandel<BiqupaiComSpider>>();
    //    services.AddSingleton<ClientBuildHelper<BiqupaiComSpider>>();

    //    services.AddHttpClient(keys[0], client =>
    //    {
    //        client.BaseAddress = new Uri(domain);

    //        client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
    //    })
    //        .AddHttpMessageHandler<HttpLogHandel<BiqupaiComSpider>>()
    //    .AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)))
    //    .ConfigurePrimaryHttpMessageHandler(() =>
    //    {
    //        return new HttpClientHandler()
    //        {
    //            AutomaticDecompression = System.Net.DecompressionMethods.GZip,

    //        };
    //    });

    //    return services;
    //}
}
